package com.second.app.thread.threadlocal.unsafe;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author gyz
 * @date 2021/1/29 16:45
 */
public class SimpleDateFormatter {

    public static void main(String[] args) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
        Date today = new Date();
        Date tomorrow = new Date(today.getTime() + 1000 * 60 * 60 * 24);
        System.out.println(today);
        System.out.println(tomorrow);
        TaskA taskA = new TaskA(dateFormat, today);
        new Thread(taskA).start();
        TaskB taskB = new TaskB(dateFormat, tomorrow);
        new Thread(taskB).start();
    }
}

class TaskA implements Runnable {

    private SimpleDateFormat simpleDateFormat;

    private Date date;

    public TaskA(SimpleDateFormat simpleDateFormat, Date date) {
        this.simpleDateFormat = simpleDateFormat;
        this.date = date;
    }

    @Override
    public void run() {
        while (true) {
            String formatDate = simpleDateFormat.format(date);
            if (!"2021-01-29".equals(formatDate)) {
                System.out.println("today：" + formatDate);
                System.exit(0);
            }
        }

    }
}

class TaskB implements Runnable {
    private SimpleDateFormat simpleDateFormat;

    private Date date;

    public TaskB(SimpleDateFormat simpleDateFormat, Date date) {
        this.simpleDateFormat = simpleDateFormat;
        this.date = date;
    }

    @Override
    public void run() {
        while (true) {
            String formatDate = simpleDateFormat.format(date);
            if (!"2021-01-30".equals(formatDate)) {
                System.out.println("tomorrow：" + formatDate);
                System.exit(0);
            }
        }
    }

}